package com.wxy.controller;

import com.wxy.domain.MyException;
import jakarta.servlet.http.HttpServletRequest;
import jdk.dynalink.beans.StaticClass;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author 47868
 */
@Controller
@RequestMapping("file")
public class FileUpController {
    public static void println(Object message){
        System.out.println(message);
    }

    @RequestMapping("filehtml")
    public String fileupload() {
        return "FileUpload";
    }
    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        println("正在处理...");
        if(file.isEmpty()){
            return "上传失败";
        }
        String fileName=file.getOriginalFilename();
        String path="D:/代码/upload/"+fileName;
        File filePath = new File(path);
        try {
            file.transferTo(filePath);
        } catch (IOException e) {
            throw new MyException(e.getMessage());
        }
        return "上传成功";
    }
}
